//
//  HLTUpdatapasswordTableViewController.m
//  huiliantong
//
//  Created by hlt on 15/8/31.
//  Copyright (c) 2015年 hlt. All rights reserved.
//

#import "HLTUpdatapasswordTableViewController.h"
#import "HLTUpdatapasswordTableViewCell.h"
@interface HLTUpdatapasswordTableViewController ()<MONActivityIndicatorViewDelegate>


@property (weak, nonatomic) IBOutlet UILabel *oldpwd;

@property (weak, nonatomic) IBOutlet UILabel *newpwd;

@property (weak, nonatomic) IBOutlet UILabel *aginpwd;

@property NSString *oldpwdstr;
@property NSString *newpwdstr;

@property NSString *aginpwdstr;

@end

@implementation HLTUpdatapasswordTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置返回的按钮的字体的颜色的
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
    //设置导航的背景颜色的
    self.navigationController.navigationBar.barTintColor = navigationBarColor;
    
    self.oldpwdstr=@"";
    self.newpwdstr=@"";
    self.aginpwdstr=@"";
    
    
    //    self.navigationController.title=@"登陆";
    [self.navigationItem setTitle:@"修改登陆密码"];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        if (indexPath.row==0)
        {
            [EYInputPopupView popViewWithTitlenumber:@"填写原密码" contentText:self.oldpwd.text
                                                type:EYInputPopupView_Type_single_line_text
                                         cancelBlock:^{
                                             
                                         } confirmBlock:^(UIView *view, NSString *text) {
                                             
                                             self.oldpwdstr=text;
                                             self.oldpwd.text=@"******";
                                         } dismissBlock:^{
                                             
                                         }];
        }else if (indexPath.row==1)
        {
            [EYInputPopupView popViewWithTitlenumber:@"设置新密码(限制为6位)" contentText:self.newpwd.text
                                                type:EYInputPopupView_Type_single_line_text
                                         cancelBlock:^{
                                             
                                         } confirmBlock:^(UIView *view, NSString *text) {
                                              self.newpwd.text=@"******";
                                             self.newpwdstr=text;
                                         } dismissBlock:^{
                                             
                                         }];
        }else if (indexPath.row==2)
        {
            [EYInputPopupView popViewWithTitlenumber:@"确认密码" contentText:self.aginpwd.text
                                                type:EYInputPopupView_Type_single_line_text
                                         cancelBlock:^{
                                             
                                         } confirmBlock:^(UIView *view, NSString *text) {
                                              self.aginpwd.text=@"******";
                                             self.aginpwdstr=text;
                                         } dismissBlock:^{
                                             
                                         }];
        }
        
        
        
        
        
    }
    
    
    
    
    
}














- (IBAction)tijiao:(id)sender {
    
    
    if ([self.oldpwdstr isEqual:@"原密码"]) {
        
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"请输入原密码"   message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
        [alert show];
        
        
    }else if ([self.oldpwdstr length]!=6)
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"原密码长度必须6位"   message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
        [alert show];
        
    }else if ([self.newpwdstr length]!=6)
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"新密码长度必须6位"   message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
        [alert show];
        
    }else if (![self.newpwdstr isEqual:self.oldpwdstr])
    {
        if (![self.newpwdstr isEqual:self.aginpwdstr]) {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"新密码和确认密码必须相同"   message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
            [alert show];
            
        }
        else
        {
            if ([self.newpwdstr isEqual:@"新密码"]) {
                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"必须设置新密码"   message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
                [alert show];
            }
            else{
                
                
                
                UIView *view1=[[UIView alloc] initWithFrame:self.view.bounds];
                view1.backgroundColor=[UIColor grayColor];
                view1.alpha=0.4;
                [self.view addSubview:view1];
                MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
                indicatorView.delegate = self;
                indicatorView.numberOfCircles = 3;
                indicatorView.radius = 20;
                indicatorView.internalSpacing = 3;
                indicatorView.center = self.view.center;
                [indicatorView startAnimating];
                [self.view addSubview:indicatorView];
                
                
            
                
                
                Account *acc=[[AccountTool sharedAccountTool] currentAccount];
                
                //base64 编码
                NSString *base64oldpwd= [SetBase64andMd5 base64:self.oldpwdstr ncryptOrDecrypt:kCCEncrypt];
                
                //base64 编码
                NSString *base64newpwd= [SetBase64andMd5 base64:self.newpwdstr ncryptOrDecrypt:kCCEncrypt];
                
                
                
                NSString *url=[xiugaipwd stringByAppendingFormat:@"&username=%@&password=%@&newpwd=%@",acc.custPhone,base64oldpwd,base64newpwd];
                
                [HLTNetWorkingtool getary:url success:^(NSArray *ary) {
                    
                                        NSLog(@"%@",ary[0][@"status"]);
                    [indicatorView stopAnimating];
                    [view1 removeFromSuperview];
                    //将nsnumber 装换成nsstring
//                    NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
//                    NSString *str = [numberFormatter stringFromNumber:ary[0][@"status"]];
                    
                    if ([ary[0][@"status"] isEqual:@"0"]) {
                        //返回上一个页面
                        [self.navigationController popViewControllerAnimated:YES];
                        //                        [self.navigationController popViewControllerAnimated:YES]; //返回上一页面
                        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"登录密码修改成功"   message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
                        [alert show];
                        
                    }
                    else
                    {
                        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"原密码输入错误，请重试"   message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
                        [alert show];
                        
                    }
                    
                } faliure:^(NSString *error) {
                    
                    [indicatorView stopAnimating];
                    [view1 removeFromSuperview];
                    
                }];
                
            }
            
        }
        
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"新密码不能与原密码相同"  message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
        [alert show];
        
    }

    
    
    
    
    
    
    
}






//#pragma mark - Table view data source
//
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    // Return the number of sections.
//    return 1;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    // Return the number of rows in the section.
//    return 1;
//}
//
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    //    static NSString *identify1 = @"HLTloginTableViewCell";
//    static NSString *identify1 = @"HLTUpdatapasswordTableViewCell";
//    
//    //    HLTloginTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify1];
//    HLTUpdatapasswordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify1];
//    if(cell == nil)
//    {
//        //        cell= [[[NSBundle mainBundle]loadNibNamed:@"HLTloginTableViewCell" owner:nil options:nil] firstObject];
//        cell= [[[NSBundle mainBundle]loadNibNamed:@"HLTUpdatapasswordTableViewCell" owner:nil options:nil] firstObject];
//    }
//    //    cell.backgroundColor=[UIColor clearColor];//关键语句
//    cell.selectionStyle=UITableViewCellSelectionStyleNone;
//    return cell;
//}
//
//
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return screen_height;
//    
//}

@end
